//
//  ModifyProjectTableViewController.swift
//  XMGG
//
//  Created by iDev on 6/26/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit

class ModifyProjectView: UIView, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, ModifyProjectCellDelegate {
    let tableView = UITableView(frame: CGRect.zero, style: .grouped)
    let headView = UIView()
    let footView = UIView()
    
    let projectLable = UITextField()
    
    let delBtn = UIButton()
    let editBtn = UIButton()
    let saveBtn = UIButton()
    
    var modle:ModifyProjectModle?
    var originModle:ModifyProjectModle?
    
    var delegate:ModifyProjectViewDelegate?
    
    var mode:ProjectMode?
    
    init(frame: CGRect, modle:ModifyProjectModle, mode:ProjectMode) {
        super.init(frame: frame)
        self.modle = modle
        self.originModle = modle
        
        self.mode = mode
        setViews()
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    func reloadData(){
        tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "modifycell") as? ModifyProjectTableViewCell
        if cell == nil{
            cell = ModifyProjectTableViewCell(style: .default, reuseIdentifier: "modifycell")
        }
        cell?.mode = mode!
        cell?.selectionStyle = .none
        cell?.delegate = self
        cell?.setCellTitle(detail: modle!.projectDetail[indexPath.section][indexPath.row], index: indexPath)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 47.0
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modle!.projectDetail[section].count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return modle!.projectDetail.count
    }
    
    func setViews(){
        addSubview(tableView)
        addSubview(footView)
        setHeadAndFoot()
        tableView.backgroundColor = UIColor(red: 240/255.0, green: 245/255.0, blue: 245/255.0, alpha: 1)
        tableView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(footView.snp.top)
        }
    }
    
    func setHeadAndFoot(){
        let timeLable = UILabel()
        timeLable.text = modle?.projectTime
        timeLable.font = UIFont.init(name: "PingFang SC", size: 15*0.75)
        timeLable.textColor = UIColor(red: 56/255.0, green: 124/255.0, blue: 124/255.0, alpha: 1)
        timeLable.sizeToFit()
        headView.addSubview(timeLable)
        
        let sepreateLine = UIView()
        sepreateLine.backgroundColor = UIColor(red: 217/255.0, green: 217/255.0, blue: 217/255.0, alpha: 1.0)
        headView.addSubview(sepreateLine)
        sepreateLine.snp.makeConstraints { (make) in
            make.top.equalTo(timeLable.snp.bottom).offset(-1)
            make.height.equalTo(1)
        }
    
        projectLable.adjustsFontSizeToFitWidth = true
        projectLable.returnKeyType = .done
        projectLable.delegate = self
        projectLable.clearButtonMode = .whileEditing
        let (text, enable) = (modle?.projectName)!
        projectLable.text = text
        projectLable.isEnabled = false
        projectLable.placeholder = text
        
        if mode == .add && enable{
            projectLable.isEnabled = true
            projectLable.text = ""
        }
        
        projectLable.font = UIFont.init(name: "PingFang SC", size: 16)
        projectLable.sizeToFit()
        headView.addSubview(projectLable)
        
        timeLable.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(8)
            make.left.equalToSuperview().offset(23)
        }
        projectLable.snp.makeConstraints { (make) in
            make.left.equalTo(timeLable)
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(52)
        }
        
        headView.frame = CGRect(x: 0, y: 0, width: SCREENWIDTH, height: projectLable.frame.height+8+47)
        tableView.tableHeaderView = headView
        
        delBtn.setImage(#imageLiteral(resourceName: "toolbar_btndel"), for: .normal)
        editBtn.setImage(#imageLiteral(resourceName: "toolbar_btnmod"), for: .normal)
        saveBtn.setImage(#imageLiteral(resourceName: "toolbar_btnsave"), for: .normal)
        delBtn.backgroundColor = UIColor.white
        editBtn.backgroundColor = UIColor.white
        saveBtn.backgroundColor = UIColor.white
        editBtn.addTarget(self, action: #selector(editBtnClick), for: .touchUpInside)
        saveBtn.addTarget(self, action: #selector(saveBtnClick), for: .touchUpInside)
        if mode == .add {
            editBtn.isHidden = true
        }else{
            saveBtn.isHidden = true
        }
        
        footView.addSubview(delBtn)
        footView.addSubview(editBtn)
        footView.addSubview(saveBtn)
        
        footView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(43)
        }
        delBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        editBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(1.5)
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview()
        }
        saveBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview().multipliedBy(1.5)
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview()
        }
    }
    
    func editBtnClick(){
        saveBtn.isHidden = false
        editBtn.isHidden = true
        mode = .edit
        let (_, enable) = modle!.projectName
        if enable {
            projectLable.textColor = UIColor(red: 135/255.0, green: 137/255.0, blue: 137/255.0, alpha: 1)
            projectLable.isEnabled = true
        }
        tableView.reloadData()
        delegate?.editBtnClick()
    }
    
    func saveBtnClick(){
        firstRespondView?.resignFirstResponder()
        delegate?.saveBtnClick(modle: modle!)
    }
    
    func modifyText(section: Int, row: Int, data: String) {
        modle?.projectDetail[section][row].2 = data
    }
    
    func keyboardCoverCell(offset: CGFloat) {
        tableView.setContentOffset(CGPoint(x:0,y:offset), animated: true)
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        firstRespondView = textField
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        modle?.projectName = (textField.text ?? "", true)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        firstRespondView?.resignFirstResponder()
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        projectLable.textColor = UIColor.black
        return true
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        firstRespondView?.resignFirstResponder()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

protocol ModifyProjectViewDelegate {
    func editBtnClick()
    func saveBtnClick(modle:ModifyProjectModle)
}

